package cn.zhfeat.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class FastJsonUtil {
	public FastJsonUtil() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 创建json格式数据
	 * 
	 * @param value
	 * @return
	 */
	public static String createJsonString(Object value) {
		JSONObject JSONObject = new JSONObject();
		String str = JSONObject.toJSONString(value);
		return str;
	}

	/**
	 * 解析json转换为pojo对象
	 * 
	 * @param clazz
	 * @param json
	 * @return
	 */
	public static Object jsonToPojo(Class<?> clazz, String json) {
		Object obj = null;
		JSONObject JSONObject = new JSONObject();
		obj = JSONObject.parseObject(json, clazz);
		return obj;
	}

	/**
	 * 解析json转换为pojo对象集合
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> jsonToList(String json, Class<T[]> clazz) {
		JSONObject JSONObject = new JSONObject();
		T[] array = JSONObject.parseObject(json, clazz);
		return Arrays.asList(array);
	}

	/**
	 * 解析json转换为list集合
	 * 
	 * @param json
	 * @param clazz
	 * @return
	 */
	/*
	 * public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
	 * Type type = new TypeToken<ArrayList<JsonObject>>() { }.getType();
	 * ArrayList<JsonObject> jsonObjects = new JSONObject().fromJson(json, type);
	 * 
	 * ArrayList<T> arrayList = new ArrayList<>(); for (JsonObject jsonObject :
	 * jsonObjects) { arrayList.add(new JSONObject().fromJson(jsonObject, clazz)); }
	 * return arrayList; }
	 */

	/**
	 * json转List<Map<String,Object>>
	 */

	public static List<Map<String, Object>> jsonToMapList(String jsonArrayData) {

		JSONArray jsonArray = JSONArray.parseArray(jsonArrayData);

		List<Map<String, Object>> mapListJson = (List) jsonArray;
		System.out.println(mapListJson.size());

		return mapListJson;
	}

	/*
	 * public static Map<String, Object> json2map(String str_json) { Map<String,
	 * Object> res = null; try { JSONObject JSONObject = new JSONObject(); res =
	 * JSONObject.fromJson(str_json, new TypeToken<Map<String, Object>>() {
	 * }.getType()); } catch (JsonSyntaxException e) { } return res; }
	 */
	public static Map<String, Object> objectToMap(Object obj) throws Exception {
		if (obj == null)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();

		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
			String key = property.getName();
			if (key.compareToIgnoreCase("class") == 0) {
				continue;
			}
			Method getter = property.getReadMethod();
			Object value = getter != null ? getter.invoke(obj) : null;
			map.put(key, value);
		}

		return map;
	}

	public static Map<String, Object> json2map(String str_json) {
		Map<String, Object> res = JSON.parseObject(str_json);
		return res;
	}

	public static Map<String, String> json2mapStr(String str_json) {
		Map<String, String> data = new HashMap<String, String>();
		JSONObject jsonObject = JSONObject.parseObject(str_json);
		Set<String> ite = jsonObject.keySet();
		for (String key : ite) {
			data.put(key, jsonObject.getString(key));
		}
		return data;
	}

	public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
		ArrayList<T> ts = (ArrayList<T>) JSONArray.parseArray(json, clazz);
		return ts;
	}
}
